"""电影应用测试工厂模块

定义了用于创建测试数据的工厂类。
"""

import factory
from django.contrib.auth import get_user_model
from factory import Faker
from factory.django import DjangoModelFactory

from movies.models import Movie


class MovieFactory(DjangoModelFactory):
    """电影工厂类，用于创建测试用的电影实例"""

    class Meta:
        model = Movie
        skip_postgeneration_save=True

    title = Faker("sentence", nb_words=4)  # 电影标题
    genres = Faker(
        "pylist", nb_elements=3, variable_nb_elements=True, value_types=["str"]
    )  # 电影类型列表


class UserFactory(factory.django.DjangoModelFactory):
    """用户工厂类，用于创建测试用的用户实例"""

    class Meta:
        model = get_user_model()
        skip_postgeneration_save=True

    username = factory.Sequence(lambda n: "user_%d" % n)  # 用户名
    email = factory.LazyAttribute(lambda obj: "%s@example.com" % obj.username)  # 邮箱
    password = factory.PostGenerationMethodCall("set_password", "defaultpassword")  # 密码
